<?php
/**
 * Processor for building request export queue
 * @package modules.dept_pharmacy.includes.EwhExport
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class EwhReqExportQueueProc {
    private $controller;
    private $logger ;
    function __construct($controller) {
        $this->controller = $controller;
        $this->logger = LoggerManager::getLogger('modules.dept_pharmacy.includes.EwhExport.EwhReqExportQueueProc');
    }

    public function listPendingRequests($filter) {
        $phaReqExticketObj = Celini::newOrdo('PhaReqExticket');
        $arrPendingReq = $phaReqExticketObj->listPendingRequests($filter);
        if ($arrPendingReq != null) {
            $this->decoratePendingRequestsData($arrPendingReq);
        }
        return $arrPendingReq;
    }

    public function getRequest($reqId, $filter) {
        $phaReqExticketObj = Celini::newOrdo('PhaReqExticket');
        $req = $phaReqExticketObj->getRequest($reqId, $filter);
        if ($req != null) {
            
            $this->decorateARequestData($req);
        }
        return $req;
    }
    private function decoratePendingRequestsData(&$data = array()) {
        $departmentObj = Celini::newOrdo('Department');
        $listDept = $departmentObj->listAll();
        $phaDrugStoreObj = Celini::newOrdo('PhaDrugStore');
        $listDrugStore = $phaDrugStoreObj->listAll();

        foreach ($data as &$item) {
            $item['req_date_formatted'] = L10nHelper::renderDateTime($item['req_date']);
            $item['source_name'] = '';

            if ($item['enum_source_type'] == EnumerationConstant::PHA_REQ_SOURCE_TYPE_DEPT_KEY
            && isset($listDept[$item['source_id']])) {

                $item['source_name'] = $listDept[$item['source_id']];

            } else if ($item['enum_source_type'] == EnumerationConstant::PHA_REQ_SOURCE_TYPE_DRUG_STORE_KEY
            && isset($listDrugStore[$item['source_id']])) {

                $item['source_name'] = $listDrugStore[$item['source_id']];

            }
        }
    }

    public function listProcessedRequests($filter) {
        $phaReqExticketObj = Celini::newOrdo('PhaReqExticket');
        $arrProcessedReq = $phaReqExticketObj->listProcessedRequests($filter);
        if ($arrProcessedReq != null) {
            $this->decorateRequestsData($arrProcessedReq);
        }
        return $arrProcessedReq;
    }
    private function decorateRequestsData(&$data = array()) {
        $departmentObj = Celini::newOrdo('Department');
        $listDept = $departmentObj->listAll();
        $phaDrugStoreObj = Celini::newOrdo('PhaDrugStore');
        $listDrugStore = $phaDrugStoreObj->listAll();

        foreach ($data as &$item) {
            $item['req_date_formatted'] = L10nHelper::renderDateTime($item['req_date']);
            $item['app_date_formatted'] = L10nHelper::renderDateTime($item['app_date']);

            $item['source_name'] = '';

            if ($item['enum_source_type'] == EnumerationConstant::PHA_REQ_SOURCE_TYPE_DEPT_KEY
            && isset($listDept[$item['source_id']])) {

                $item['source_name'] = $listDept[$item['source_id']];

            } else if ($item['enum_source_type'] == EnumerationConstant::PHA_REQ_SOURCE_TYPE_DRUG_STORE_KEY
            && isset($listDrugStore[$item['source_id']])) {

                $item['source_name'] = $listDrugStore[$item['source_id']];

            }
        }

    }
    private function decorateARequestData(&$item) {
        $departmentObj = Celini::newOrdo('Department');
        $listDept = $departmentObj->listAll();
        $phaDrugStoreObj = Celini::newOrdo('PhaDrugStore');
        $listDrugStore = $phaDrugStoreObj->listAll();

        $item['req_date_formatted'] = L10nHelper::renderDateTime($item['req_date']);
        
        $item['source_name'] = '';

        if ($item['enum_source_type'] == EnumerationConstant::PHA_REQ_SOURCE_TYPE_DEPT_KEY
        && isset($listDept[$item['source_id']])) {

            $item['source_name'] = $listDept[$item['source_id']];

        } else if ($item['enum_source_type'] == EnumerationConstant::PHA_REQ_SOURCE_TYPE_DRUG_STORE_KEY
        && isset($listDrugStore[$item['source_id']])) {

            $item['source_name'] = $listDrugStore[$item['source_id']];

        }


    }

}
?>